package com.gxa.modules.payment.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.modules.payment.entity.MeterDetails;
import com.gxa.modules.payment.form.MeterDetailsForm;
import com.gxa.modules.payment.form.PayItemForm;
import com.gxa.modules.payment.service.MeterDetailsService;
import com.gxa.modules.payment.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

@Api(tags = "抄表明细 接口")
@RestController
public class MeterDetailsController {

    @Autowired
    private MeterDetailsService meterDetailsService;

    @ApiOperation("抄表明细 查询")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "page",value = "当前第几页"),
            @ApiImplicitParam(paramType = "query",name = "limit",value = "每页显示多少数据"),

            @ApiImplicitParam(paramType = "query",name = "managementName",value = "查询条件 住户姓名"),
            @ApiImplicitParam(paramType = "query",name = "room",value = "查询条件 房间号"),
            @ApiImplicitParam(paramType = "query",name = "meterName", value = "查询条件 仪表名称"),
    })
    @GetMapping("/meterDetails/list")
    public Result<List<MeterDetails>> listResult(@RequestParam @ApiIgnore Map<String,Object> params){
        Result<List<MeterDetails>> r = Result.success();

        String page1 = (String) params.get("page");
        String limit1 = (String) params.get("limit");

        Integer page = Integer.parseInt(page1);
        Integer limit = Integer.parseInt(limit1);

        try{
            PageHelper.startPage(page,limit);
            List<MeterDetails> meterDetails = this.meterDetailsService.queryByPage(params);
            PageInfo<MeterDetails> pageInfo = new PageInfo<>(meterDetails);

            long total = (int)pageInfo.getTotal();
            return Result.success(meterDetails,total);
        } catch (Exception e){
            r.setCode("1");
            r.setMsg("error");
            e.printStackTrace();
        }
        return r;
    }


    @ApiOperation("抄表明细管理 删除多条")
    @DeleteMapping("/MeterDetails/delete")
    public Result MeterDetailsDelete(@RequestBody List<Integer> ids){
        Result r = new Result<>();

        try{
            this.meterDetailsService.MeterDetailsDelete(ids);
            return Result.success();
        } catch (Exception e){
            r.setCode("1");
            r.setMsg("error");
            e.printStackTrace();
        }
        return r;
    }



    @GetMapping("/meterDetailsForm/list")
    @ApiOperation("抄表明细 详情")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "id", value = "查询条件 抄表明细id"),
    })
    public Result<List<MeterDetailsForm>> queryById(Integer id){
        Result<List<MeterDetailsForm>> r = Result.success();

        try{
            List<MeterDetailsForm> meterDetailsForms = this.meterDetailsService.queryById(id);
            PageInfo<MeterDetailsForm> pageInfo = new PageInfo<>(meterDetailsForms);

            long total = (int)pageInfo.getTotal();
            return Result.success(meterDetailsForms,total);
        } catch (Exception e){
            r.setCode("1");
            r.setMsg("error");
            e.printStackTrace();
        }
        return r;
    }
}
